<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>
<%@include file="chksiteuser.jsp"%>
<%@ include file="../connstring.jsp"%>
<%
request.setCharacterEncoding("utf-8");
String strId =request.getParameter("id");
String strUs =request.getParameter("us");
String strMm =request.getParameter("pa");
if(strUs==null) strUs="";
if(strMm==null) strMm="";
if(strUs.equals("")||strMm.equals("")){
%>
<script type="text/javascript">
alert('有一项内容为空!\n请重试……');
history.go(-1);
</script>
<%
}
else{
	//strId、strUs、strMm 分别表示记录的 ID、用户名、密码
		Connection conn = null;
		PreparedStatement stmt = null;

		try {
		    Class.forName(driver);
		    conn = DriverManager.getConnection(url);
		    String sql = "UPDATE user SET username = ?, password = ? WHERE id = ?";
		    stmt = conn.prepareStatement(sql);
		    stmt.setString(1, strUs);
		    stmt.setString(2, strMm);
		    stmt.setInt(3, Integer.parseInt(strId));
		    int num = stmt.executeUpdate();
		    if (num > 0) {
		        // 更新成功
		        out.println("更新成功！");
		        response.sendRedirect("userlist.jsp");
		    } else {
		        // 更新失败
		        out.println("更新失败！");
		        response.sendRedirect("useredit.jsp");
		    }
		} catch (Exception e) {
		    e.printStackTrace();
		} finally {
		    if (stmt != null) {
		        try {
		            stmt.close();
		        } catch (SQLException e) {
		            e.printStackTrace();
		        }
		    }
		    if (conn != null) {
		        try {
		            conn.close();
		        } catch (SQLException e) {
		            e.printStackTrace();
		        }
		    }
		}
	}
	%>